const { ccclass, property } = cc._decorator;

@ccclass
export default class CCCompentHelper extends cc.Component {

    /**
     * 获取父类下任意子孙节点
     * @param parentNode 父节点名称
     * @param childName 子孙节点名称
     */
    public static FindChildNode(parentNode: cc.Node, childName: string): cc.Node {
        var self = this;
        let chaildNodes: cc.Component[] = null;
        var childNode: cc.Node = null;
        chaildNodes = parentNode.getComponentsInChildren(cc.Component);
        for (var i = 0; i < chaildNodes.length; i++) {
            var element = chaildNodes[i];
            var curNodeName = element.name;
            var index = curNodeName.indexOf('<');
            if (index >= 0) {
                curNodeName = curNodeName.substr(0, index);
            }
            if (curNodeName == childName) {
                childNode = element.node;
                break;
            }
        }
        return childNode;
    }

    /**获取节点上任意组件 */
    public static GetComponent<T>(t: { new(): T }, node: cc.Node) {
        let item: T = node.getComponent(t);
        if (item) {
            return item;
        }
        cc.log(node.name + "没有组件" + t.toString());
        return null;
    }


    /**
     * 重新设置任意节点的父对象
     * @param newParent 新父节点
     * @param childName 子节点名称
     * @param oldParent 旧父节点,可选,不填从Canvas查找,耗!
     */
    public static SetNodeParent(newParent: cc.Node, childName: string, oldParent?: cc.Node) {
        if (oldParent == null) {
            oldParent = cc.find("Canvas") || cc.find("Root");
        }
        let chaildNode: cc.Node = this.FindChildNode(oldParent, childName);
        chaildNode.parent = newParent;
    }

    /**
     * 按钮事件 1:父物体 2:按钮名称 3:回调
     * @param parent 
     * @param btnName 
     * @param callBack 
     */
    public static RigisterButtonObjectEvent(parent: cc.Node, btnName: string, callBack: Function) {
        var btnNode: cc.Node = CCCompentHelper.FindChildNode(parent, btnName);
        btnNode.on(cc.Node.EventType.TOUCH_START, function () {
            //btnNode.runAction(cc.scaleTo(0.9, 0.9));
        });
        btnNode.on(cc.Node.EventType.TOUCH_END, function () {
            callBack();
            //btnNode.runAction(cc.scaleTo(0.1,1.1, 1.1));
            //btnNode.runAction(cc.scaleTo(0.2,1, 1));
        });
    }

    /**
     *复选按钮注册事件     
     *1.toggle:复选组件                
     *2.target:脚本组件(回调脚本所在组件)     
     *3.HandlerName:回调      
     *4.args:参数,可切割组合多个参数
     */
    public static RisiterToggleObjectEvent(toggle: cc.Toggle, target: cc.Node, HandlerName: string, args: string = "") {
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = target.name;
        eventHandler.handler = HandlerName;
        eventHandler.customEventData = args;
        toggle.checkEvents.push(eventHandler);
    }

    /**
    *按钮注册事件     
    *1.btn:按钮组件                
    *2.target:脚本组件(回调脚本所在组件)     
    *3.HandlerName:回调      
    *4.args:参数,可切割组合多个参数
    */
    public static RisiterButtonObjectEvent(btn: cc.Button, target: cc.Node, HandlerName: string, args: string = "") {
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = target.name;
        eventHandler.handler = HandlerName;
        eventHandler.customEventData = args;
        btn.clickEvents.push(eventHandler);
    }

    /**
    *注册输入框开始输入事件     
    *1.editor:输入框组件                
    *2.target:脚本组件(回调脚本所在组件)     
    *3.HandlerName:回调      
    *4.args:参数,可切割组合多个参数
    */
    public static RisiteEditingDidBeganObjectEvent(editor: cc.EditBox, target: cc.Node, HandlerName: string, args: string = "") {
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = target.name;
        eventHandler.handler = HandlerName;
        eventHandler.customEventData = args;
        editor.editingDidBegan.push(eventHandler);
    }

    /**
     *注册输入框结束输入事件     
     *1.editor:输入框组件                
     *2.target:脚本组件(回调脚本所在组件)     
     *3.HandlerName:回调      
     *4.args:参数,可切割组合多个参数
     */
    public static RisiteEditingDidEndedObjectEvent(editor: cc.EditBox, target: cc.Node, HandlerName: string, args: string = "") {
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = target.name;
        eventHandler.handler = HandlerName;
        eventHandler.customEventData = args;
        editor.editingDidEnded.push(eventHandler);
    }

    /**
    *注册输入框输入事件     
    *1.editor:输入框组件        
    *2.target:脚本组件(回调脚本所在组件)     
    *3.HandlerName:回调      
    *4.args:参数,可切割组合多个参数
    */
    public static RisiteEditorTextChangedObjectEvent(editor: cc.EditBox, target: cc.Node, HandlerName: string, args: string = "") {
        let eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = target.name;
        eventHandler.handler = HandlerName;
        eventHandler.customEventData = args;
        editor.textChanged.push(eventHandler);
    }
}
